package ferus.tigris.lines.personages;

import ferus.tigris.lines.GameView;
import ferus.tigris.lines.Matrix;
import android.graphics.Point;
import android.graphics.Rect;

public class Mark extends BehaviorDelegate {

	private int elWidth;
	private int elHeight;
	private Matrix matrix;
	private Rect screen = new Rect();
	private Point pos = new Point(0, 0);

	public Mark(BehaviorDelegate delegate, GameView gameView, Rect screen) {
		super(delegate);
		this.screen = screen;
	}

	public void onUpdate() {
	}

	public void onSavedAt(Matrix m, Point pos) {
		matrix = m;
		elWidth = (int) screen.width() / matrix.columns();
		elHeight = (int) screen.height() / matrix.rows();
		
		doSetViewSize();
		
		this.pos  = new Point(pos.x, pos.y);
		int x = pos.x*elWidth;
		int y = pos.y*elHeight;
		doSetViewCenter(x, y);
	}

	private void doSetViewSize() {
		rootParent().setViewSize(elWidth);
	}

	protected void doSetViewCenter(int x, int y) {
		x += screen.left;
		y += screen.top;
		rootParent().setViewCenter(x + elWidth/2, y + elHeight/2);
	}

	public void onMoveFinish(Point p) {
	}

	protected boolean isMyTouchEvent(Point p) {
		int x = p.x / elWidth % matrix.columns();
		int y = p.y / elHeight % matrix.rows();
		if(y < 0)y += matrix.rows();
		if(x < 0)x += matrix.columns();
		return ((pos.x == x)&&(pos.y == y));
	}

}